package com.myown.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myown.config.RedisConfig;
import com.myown.exception.GlobalException;
import com.myown.mapper.UserMapper;
import com.myown.pojo.User;
import com.myown.service.IUserService;
import com.myown.utils.CookieUtil;
import com.myown.utils.MD5Util;
import com.myown.utils.UUIDUtil;
import com.myown.vo.LoginVo;
import com.myown.vo.RespBean;
import com.myown.vo.RespBeanEnum;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hardy
 * @since 2022-05-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public RespBean doLongin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {

        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        //参数校验
//        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        if(!ValidatorUtil.isMobile(mobile)){
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }

        User user = userMapper.selectById(mobile);
        if(null == user){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }

        if(!MD5Util.formPassToDBPass(password,user.getSalt()).equals(user.getPassword())){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }

        //生成cookie
        String ticket = UUIDUtil.uuid();
//        request.getSession().setAttribute(ticket,user);
        //将用户信息存入redis
        redisTemplate.opsForValue().set("user:" + ticket, user);
        CookieUtil.setCookie(request,response,"userTicket",ticket);

        return RespBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket,HttpServletRequest request, HttpServletResponse response) {
        if(StringUtils.isEmpty(userTicket)){
            return null;
        }
        User user = (User)redisTemplate.opsForValue().get("user:"+userTicket);
        if(user != null){
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }
        return user;
    }

    //更新密码，其实没有用到，只是为了说明在redis中的数据有变更后，如何保证redis中的数据为最新值
    //根据数据库更新结果来进行区分操作，如果数据库数据更改成功，则根据返回值1来进行redis中数据的删除
//    @Override
//    public RespBean updatePassword(String userTicket, String password, HttpServletRequest request, HttpServletResponse response) {
//        TUser user = getUserByCookie(userTicket, request, response);
//        if (user == null) {
//            throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST);
//        }
//        user.setPassword(MD5Util.inputPassToDBPass(password, user.getSalt()));
//        int result = tUserMapper.updateById(user);
//        if (1 == result) {
//            //删除Redis
//            redisTemplate.delete("user:" + userTicket);
//            return RespBean.success();
//        }
//        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
//    }

}
